package com.liuji.mall.mbg.config;

import io.swagger.annotations.ApiModelProperty;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.springframework.beans.factory.annotation.Value;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


public class MyCommentGenerator extends DefaultCommentGenerator {
    private Properties properties;
    private boolean suppressDate;
    private boolean suppressAllComments;
    private boolean addRemarkComments;
    private SimpleDateFormat dateFormat;
    @Value("${comment.author}")
    private String author;
    private static final String MAPPER_SUFFIX = "Mapper";
    private static final String API_MODEL_FULL_CLASS_NAME = "io.swagger.annotations.ApiModel";
    private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME = "io.swagger.annotations.ApiModelProperty";


    public MyCommentGenerator() {
        this.properties = new Properties();
        this.suppressDate = false;
        this.suppressAllComments = false;
        this.addRemarkComments = false;
        this.dateFormat = new SimpleDateFormat("YYYY-mm-dd HH:MM:ss");
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (suppressAllComments) {
            return;
        }
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine("* this model was generated by mybatis-generator");
        topLevelClass.addJavaDocLine("* table:" + introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine("* author:" + author);
        topLevelClass.addJavaDocLine("* date:" + getDateString());
        topLevelClass.addJavaDocLine("*/");
        topLevelClass.addAnnotation("@ApiModel(\"" + introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "\")");

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        field.addJavaDocLine("/**");
        field.addJavaDocLine("* column:" + introspectedColumn.getActualColumnName());
        field.addJavaDocLine("* data type:" + introspectedColumn.getJdbcTypeName());
        field.addJavaDocLine("*/");
        field.addJavaDocLine("@ApiModelProperty(\"" + introspectedColumn.getJavaProperty() + "\")");
    }

    @Override
    protected String getDateString() {
        if (suppressDate) {
            return null;
        } else if (dateFormat != null) {
            return dateFormat.format(new Date());
        } else {
            return new Date().toString();
        }
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
        if (!compilationUnit.getType().getFullyQualifiedName().contains(MAPPER_SUFFIX)) {
            compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_FULL_CLASS_NAME));
            compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
        }
    }
}
